package com.tencent.wxcloudrun.service;

import com.tencent.wxcloudrun.dao.CouponCateMapper;
import com.tencent.wxcloudrun.dao.CouponUserMapper;
import com.tencent.wxcloudrun.model.Coupon;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: CouponUserService
 * Package: com.tencent.wxcloudrun.service
 * Description:
 *
 * @Date: 2023/1/23 2:04 PM
 * @Author: wht
 */
@Slf4j
@Service
public class CouponUserService {
    @Autowired
    CouponCateMapper couponCateMapper;
    @Autowired
    CouponUserMapper couponUserMapper;

    public void addOneCouponUser(Integer user_id, Integer coupon_cate_id) {
//        System.out.println(coupon_cate_id);
        Coupon coupon = couponCateMapper.getOneCouponCateById(coupon_cate_id);
        coupon.setUser_id(user_id);
        if (coupon.getStart_time() == null) {
            //  如果没有开始和结束时间，则设置上去
            LocalDateTime date = LocalDateTime.now();
            coupon.setStart_time(date);
            coupon.setEnd_time(date.plusDays(coupon.getTerm()));
        }
        couponUserMapper.addOneCouponUser(coupon);
    }

    public void updateOneCouponUserById(Coupon coupon) {
        couponUserMapper.updateOneCouponUserById(coupon);
    }

    public List<Coupon> getCouponUserList(Coupon coupon) {
        List<Coupon> res = couponUserMapper.getCouponUserList(coupon);
        return res;
    }

}
